package com.ht.shortlink.project.toolkit;

import jakarta.servlet.http.HttpServletRequest;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.Date;

public class LinkUtil {

    /**
     *  获取传来的日期与当前日期的时间戳的差
     * @param date 传来的日期
     * @return 时间戳的差
     */
    public static long getTimeStamp(Date date) {
        if (date == null) {
            // Return the number of milliseconds in a month
            return 30L * 24 * 60 * 60 * 1000;
        } else {
            // Return the difference in time from the supplied date to now
            long times = date.getTime() - new Date().getTime();
            System.out.println(times);
            return times;
        }
    }

    /**
     *  获取客户端的真实ip地址
     * @param request 客户端请求
     * @return 真实ip
     */
    public static String getClientIpAddress(HttpServletRequest request) {
        String xForwardedForHeader = request.getHeader("X-Forwarded-For");
        if (xForwardedForHeader != null && !xForwardedForHeader.isEmpty()) {
            // The X-Forwarded-For header can contain a comma-separated list of IP addresses.
            // The first address in the list is the original client IP.
            return xForwardedForHeader.split(",")[0].trim();
        }

        String xRealIPHeader = request.getHeader("X-Real-IP");
        if (xRealIPHeader != null && !xRealIPHeader.isEmpty()) {
            return xRealIPHeader.trim();
        }

        // If the above headers are not present, fall back to the default method
        return request.getRemoteAddr();
    }

    /**
     * 获取用户访问操作系统
     *
     * @param request 请求
     * @return 访问操作系统
     */
    public static String getOs(HttpServletRequest request) {
        String userAgent = request.getHeader("User-Agent");
        if (userAgent.toLowerCase().contains("windows")) {
            return "Windows";
        } else if (userAgent.toLowerCase().contains("mac")) {
            return "Mac OS";
        } else if (userAgent.toLowerCase().contains("linux")) {
            return "Linux";
        } else if (userAgent.toLowerCase().contains("unix")) {
            return "Unix";
        } else if (userAgent.toLowerCase().contains("android")) {
            return "Android";
        } else if (userAgent.toLowerCase().contains("iphone")) {
            return "iOS";
        } else {
            return "Unknown";
        }
    }

    /**
     * 获取用户访问浏览器
     *
     * @param request 请求
     * @return 访问浏览器
     */
    public static String getBrowser(HttpServletRequest request) {
        String userAgent = request.getHeader("User-Agent");
        if (userAgent.toLowerCase().contains("edg")) {
            return "Microsoft Edge";
        } else if (userAgent.toLowerCase().contains("chrome")) {
            return "Google Chrome";
        } else if (userAgent.toLowerCase().contains("firefox")) {
            return "Mozilla Firefox";
        } else if (userAgent.toLowerCase().contains("safari")) {
            return "Apple Safari";
        } else if (userAgent.toLowerCase().contains("opera")) {
            return "Opera";
        } else if (userAgent.toLowerCase().contains("msie") || userAgent.toLowerCase().contains("trident")) {
            return "Internet Explorer";
        } else {
            return "Unknown";
        }
    }

    /**
     * 获取用户访问设备
     *
     * @param request 请求
     * @return 访问设备
     */
    public static String getDevice(HttpServletRequest request) {
        String userAgent = request.getHeader("User-Agent");
        if (userAgent.toLowerCase().contains("mobile")) {
            return "Mobile";
        }
        return "PC";
    }

    /**
     * 获取用户访问网络
     *
     * @param request 请求
     * @return 访问设备
     */
    public static String getNetwork(HttpServletRequest request) {
        String actualIp = getClientIpAddress(request);
        // 这里简单判断IP地址范围，您可能需要更复杂的逻辑
        // 例如，通过调用IP地址库或调用第三方服务来判断网络类型
        return actualIp.startsWith("192.168.") || actualIp.startsWith("10.") ? "WIFI" : "Mobile";
    }

    /**
     *  获取 url 中的域名
     * @param url 原始链接
     * @return 域名
     */
    public static String extractDomain(String url) {
        try {
            URI uri = new URI(url);
            String domain = uri.getHost();  // 获取域名部分

            if (domain != null && domain.startsWith("www.")) {
                domain = domain.substring(4);  // 去掉开头的 www.
            }

            return domain;
        } catch (URISyntaxException e) {
            e.printStackTrace();
            return null;  // 处理URL格式错误的情况
        }
    }
}
